package controller;

import dao.SeatDAO;
import model.Seat;
import java.util.List;

/**
 * 座位控制器
 */
public class SeatController {
    private SeatDAO seatDAO;

    public SeatController() {
        this.seatDAO = new SeatDAO();
    }

    /**
     * 根据楼层获取座位列表
     */
    public List<Seat> getSeatsByFloor(int floor) {
        try {
            if (floor == -1) {
                return seatDAO.getAllSeats();
            }
            return seatDAO.getSeatsByFloor(floor);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取座位信息失败: " + e.getMessage());
        }
    }

    /**
     * 根据楼层和房间获取座位列表 - 支持时间段查询
     */
    public List<Seat> getSeatsByFloorAndRoom(int floor, int room) {
        try {
            return seatDAO.getSeatsByFloorAndRoom(floor, room);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取座位信息失败: " + e.getMessage());
        }
    }

    /**
     * 根据楼层、房间和时间段获取座位列表
     */
    public List<Seat> getSeatsByFloorAndRoom(int floor, int room, java.sql.Date date,
            java.sql.Time startTime, java.sql.Time endTime) {
        try {
            return seatDAO.getSeatsWithStatus(date, startTime, endTime, floor, room);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取座位信息失败: " + e.getMessage());
        }
    }

    /**
     * 获取所有空闲座位
     */
    public List<Seat> getAvailableSeats() {
        try {
            return seatDAO.getAvailableSeats();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取空闲座位失败: " + e.getMessage());
        }
    }

    /**
     * 根据时间段获取空闲座位
     */
    public List<Seat> getAvailableSeats(java.sql.Date date, java.sql.Time startTime, java.sql.Time endTime) {
        try {
            return seatDAO.getAvailableSeats(date, startTime, endTime);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取空闲座位失败: " + e.getMessage());
        }
    }

    /**
     * 根据楼层和时间段获取空闲座位
     */
    public List<Seat> getAvailableSeatsByFloor(int floor, java.sql.Date date, java.sql.Time startTime,
            java.sql.Time endTime) {
        try {
            return seatDAO.getAvailableSeatsByFloor(floor, date, startTime, endTime);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取楼层空闲座位失败: " + e.getMessage());
        }
    }

    /**
     * 根据时间段获取所有座位状态
     */
    public List<Seat> getAllSeatsWithStatus(java.sql.Date date, java.sql.Time startTime, java.sql.Time endTime) {
        try {
            return seatDAO.getSeatsWithStatus(date, startTime, endTime, -1, -1);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取座位状态失败: " + e.getMessage());
        }
    }

    /**
     * 根据楼层和时间段获取座位状态
     */
    public List<Seat> getSeatsByFloorWithStatus(int floor, java.sql.Date date,
            java.sql.Time startTime, java.sql.Time endTime) {
        try {
            return seatDAO.getSeatsWithStatus(date, startTime, endTime, floor, -1);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取座位状态失败: " + e.getMessage());
        }
    }

    /**
     * 获取指定楼层的所有房间号
     */
    public List<Integer> getRoomsByFloor(int floor) {
        try {
            return seatDAO.getRoomsByFloor(floor);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取房间信息失败: " + e.getMessage());
        }
    }

    /**
     * 获取所有座位
     */
    public List<Seat> getAllSeats() {
        try {
            return seatDAO.getAllSeats();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取座位信息失败: " + e.getMessage());
        }
    }

    /**
     * 根据筛选条件获取座位
     */
    public List<Seat> getFilteredSeats(int floor, int status) {
        try {
            return seatDAO.getFilteredSeats(floor, status);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取座位信息失败: " + e.getMessage());
        }
    }

    /**
     * 根据区域和状态筛选获取座位
     */
    public List<Seat> getFilteredSeatsByArea(String area, int status) {
        try {
            return seatDAO.getFilteredSeatsByArea(area, status);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取座位信息失败: " + e.getMessage());
        }
    }

    /**
     * 获取所有区域列表
     */
    public List<String> getAllAreas() {
        try {
            return seatDAO.getAllAreas();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取区域信息失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID获取座位
     */
    public Seat getSeatById(int seatId) {
        try {
            return seatDAO.getSeatById(seatId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取座位信息失败: " + e.getMessage());
        }
    }

    /**
     * 添加座位
     */
    public boolean addSeat(Seat seat) {
        try {
            return seatDAO.insertSeat(seat);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("添加座位失败: " + e.getMessage());
        }
    }

    /**
     * 检查指定楼层是否已存在座位
     */
    public boolean checkFloorExists(int floor) {
        try {
            List<Seat> existingSeats = seatDAO.getSeatsByFloor(floor);
            return !existingSeats.isEmpty();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("检查楼层状态失败: " + e.getMessage());
        }
    }

    /**
     * 根据区域和实时状态获取座位列表（包括动态预约状态）
     */
    public List<Seat> getFilteredSeatsWithRealTimeStatus(String area, int statusFilter) {
        try {
            return seatDAO.getFilteredSeatsWithRealTimeStatus(area, statusFilter);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取座位信息失败: " + e.getMessage());
        }
    }

    /**
     * 更新座位信息
     */
    public boolean updateSeat(Seat seat) {
        try {
            return seatDAO.updateSeat(seat);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("更新座位信息失败: " + e.getMessage());
        }
    }

    /**
     * 删除座位
     */
    public boolean deleteSeat(int seatId) {
        try {
            return seatDAO.deleteSeat(seatId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("删除座位失败: " + e.getMessage());
        }
    }

    /**
     * 更新座位状态
     */
    public boolean updateSeatStatus(int seatId, int status) {
        try {
            return seatDAO.updateSeatStatus(seatId, status);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("更新座位状态失败: " + e.getMessage());
        }
    }
}
